package academic.emailClient.control.listeners;

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

import academic.emailClient.view.TreeOperator;


public class TreePopUpMenuActionListener implements ActionListener{
    JTree tree;
    JPopupMenu popup;
    Point loc;
    DefaultMutableTreeNode root;
    TreeOperator treeOpertor;
    
    /**
     * Move Direction Enum
     * @author User
     *
     */
    public enum MoveDirection{
    	Upward,
    	Downward;
    }
    
    /**
     * Constructor
     * @param tree
     * @param popupm
     * @param root
     */
    public TreePopUpMenuActionListener(JTree tree, JPopupMenu popupm, DefaultMutableTreeNode root) {
        this.tree = tree;
        this.popup = popupm;
        this.root = root;
        treeOpertor = new TreeOperator(tree, root);
        tree.addMouseListener(mouseListner);
    }
 
   public void actionPerformed(ActionEvent e) {
    	TreePath path  = tree.getPathForLocation(loc.x, loc.y);
    	String action = e.getActionCommand();
        
    	if(action.equals("MOVE FOLDER UP"))
        	treeOpertor.moveFolder(tree, path, MoveDirection.Upward);
        else if(action.equals("MOVE FOLDER DOWN"))
        	treeOpertor.moveFolder(tree, path, MoveDirection.Downward);
    }
       
    /**
     * Add a mouse listener to show a right click mouse pop up
     */
    private MouseListener mouseListner = new MouseAdapter() {
        private void showPopup(MouseEvent e) {
            if(e.isPopupTrigger()) {
                loc = e.getPoint();
                popup.show(tree, loc.x, loc.y);
            }
        }
    
    /**
     * Implement events received handlers
     */
	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		showPopup(e); 
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		showPopup(e); 
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		showPopup(e); 
	}
  };
}